﻿using System;
using System.Linq;
using Solution.BusinessObject;
using Solution.CMS.MainDSTableAdapters;
using Solution.Core;
using Solution.Utilities;

namespace Solution.CMS
{
    public partial class CategoryEditor : PageBase
    {
        private readonly MainDS _mainDs = new MainDS();
        private MainDS.CategoriesRow _categoriesRow;

        private readonly CategoriesTableAdapter _categoryTableAdapter =
            new CategoriesTableAdapter();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }
            _categoryTableAdapter.Connection = CommonBO.SqlConnection;
            _categoryTableAdapter.Connection.Open();

            _categoryTableAdapter.Fill(_mainDs.Categories);

            cboParent.DataSource =
                _mainDs
                    .Categories.Rows
                    .Cast<MainDS.CategoriesRow>()
                    .Where(row => row.CategoriesRowParent == null).ToList();
            cboParent.DataBind();

            // new
            if (Request["id"] == null)
            {
                btnSave.PostBackUrl = "/cms/categoryeditor.aspx";
            }
            // edit
            else
            {
                var id = Request["id"];

                _categoriesRow = _mainDs.Categories.FindById(Convert.ToInt32(id));
                txtTitle.Text = _categoriesRow.Title;
                txtThumbnail.Text = _categoriesRow.Thumb;
                if (_categoriesRow.CategoriesRowParent != null)
                {
                    cboParent.Value = _categoriesRow.Parent_Id;
                }
                btnSave.PostBackUrl = "/cms/categoryeditor.aspx?id=" + id;
            }

            _categoryTableAdapter.Connection.Close();

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                _categoryTableAdapter.Connection = CommonBO.SqlConnection;
                _categoryTableAdapter.Connection.Open();

                _categoryTableAdapter.Fill(_mainDs.Categories);

                // new
                if (Request["id"] == null)
                {
                    _categoriesRow = (MainDS.CategoriesRow)_mainDs.Categories.NewRow();
                    _categoriesRow.Language_Id = 1;
                    _mainDs.Categories.Rows.Add(_categoriesRow);
                }
                // edit
                else
                {
                    var id = Request["id"];

                    _categoriesRow = _mainDs.Categories.FindById(Convert.ToInt32(id));
                }

                _categoriesRow.Title = txtTitle.Text;
                _categoriesRow.Thumb = txtThumbnail.Text;
                if (cboParent.Value != null)
                {
                    _categoriesRow.Parent_Id = (int) cboParent.Value;
                }
                else
                {
                    _categoriesRow.SetParent_IdNull();
                }

                _categoryTableAdapter.Adapter.Update(_mainDs);
                _categoryTableAdapter.Connection.Close();

                Response.Redirect("default.aspx");
            }
            catch (Exception exception)
            {
                Logger.WriteLog(exception);
            }
        }
    }
}